<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/4/16
 * Time: 20:16
 */

namespace app\admin\model;
use think\Model;
use Endroid\QrCode\QrCode;
class Word extends Model
{
    protected $insert = [
        'views' => 0,
        'likes' => 0,
        'comments' => 0,
        'create_time' => NOW_TIME,
    ];

    //获取后台用户名称
    public function user(){
        return $this->hasOne('Member','id','create_user');
    }
    public function qrcode($code) {
        $url = "https://".$_SERVER['SERVER_NAME']."/home/word/review?code=".$code;
        //生成保存二维码文件路径
        $date = date("Y-m",time());
        if (!is_dir('qrcode')) mkdir('qrcode', 0777);
        if (!is_dir('qrcode/'.$date)) mkdir('qrcode/'.$date, 0777);
        $file = "qrcode/".$date."/".time().".jpg";
        //生成二维码
        $qrCode = new QrCode();
        $qrCode
            ->setText($url)
            ->setSize(300)
            ->setPadding(10)
            ->setErrorCorrection('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
            ->setLabelFontSize(16)
            ->render($file)
        ;
        $data = array(
            'code' => $code,
            'url' => $url,
            'code_path' => $file,
        );
        return json_encode($data);
    }
    /**
     * 利用时间戳及英文字母生成16位随机数
     */
    function foo() {
        $o = $last = '';
        do {
            $last = $o;
            usleep(10);
            $t = explode(' ', microtime());
            $o = substr(base_convert(strtr($t[0].$t[1].$t[1],'.',''),10,36),0,16);
        }while($o == $last);
        return $o;
    }
}